<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{
              item.pubdate | relativeTime
            }}</span>
            <van-button size="mini" type="default" @click="showCommentReply(item)"
              >回复{{ item.reply_count }}</van-button
            >
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model.trim="value"
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="addComment"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 圆角弹窗 -->

    <van-popup
      v-model="commentReply"
      round
      position="bottom"
      :style="{ height: '85%' }"
    >
    <!-- bug再次打开后不会更新数据 -->
      <comment-reply v-if='commentReply' :comment='currentComment' @showOff='commentReply=false' :artId='id'/>
    </van-popup>
  </div>
</template>

<script>
import commentReply from './commentReply.vue'
import { reqGetComment, reqAddCommentReply } from '@/api/comment'
export default {
  name: 'ArticleComment',
  components: {
    commentReply
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      value: '',
      commentReply: false,
      currentComment: {}
    }
  },
  created () {},
  methods: {
    async onLoad () {
      // 异步更新数据
      // eslint-disable-next-line camelcase
      const {
        data: {
          // eslint-disable-next-line camelcase
          data: { last_id, results }
        }
      } = await reqGetComment('a', this.id, this.offset)

      // console.log(results, last_id)
      // eslint-disable-next-line camelcase
      // eslint-disable-next-line no-unused-expressions
      this.list.push(...results)
      // eslint-disable-next-line camelcase
      this.offset = last_id
      this.loading = false
      if (!results.length) {
        this.finished = true
      }
    },
    async addComment () {
      if (!this.value) return
      try {
        // eslint-disable-next-line camelcase
        const {
          data: {
            // eslint-disable-next-line camelcase
            data: { new_obj }
          }
        } = await reqAddCommentReply(this.id, this.value)
        this.list.unshift(new_obj)
        this.$toast.fail('文章评论成功')
        this.value = ''
      } catch (error) {
        this.$toast.fail('文章禁止评论')
      }
    },
    showCommentReply (item) {
      this.commentReply = true
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
